package com.ruoyi.cpsp.service.impl;
/*
    @author neu.xiangwenjuan
    @create 2023/3/14
*/

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.cpsp.domain.CurrentPerformanceData;
import com.ruoyi.cpsp.domain.CurrentPerformanceTotal;
import com.ruoyi.cpsp.domain.PreviousPerformanceData;
import com.ruoyi.cpsp.domain.PreviousPerformanceDataEn;
import com.ruoyi.cpsp.mapper.CurrentPerformanceDataMapper;
import com.ruoyi.cpsp.mapper.EmployeeMapper;
import com.ruoyi.cpsp.mapper.PreviousPerformanceDataMapper;
import com.ruoyi.cpsp.service.PreviousPerformanceDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PreviousPerformanceDataServiceImpl implements PreviousPerformanceDataService {

    @Autowired
    private PreviousPerformanceDataMapper mapper;

    @Autowired
    private CurrentPerformanceDataMapper currentMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public int insert(PreviousPerformanceData record) {
        return mapper.insert(record);
    }

    @Override
    public int updateByPrimaryKeySelective(PreviousPerformanceData record) {
        return mapper.updateById(record);
    }

    @Override
    public List<PreviousPerformanceDataEn> selectAll(Integer year, Integer quarter) {
        return mapper.selectAll(year, quarter);
    }

    @Override
    public List<PreviousPerformanceDataEn> selectDataByIdCard(Integer year, Integer quarter, List<String> idCardList) {
        return mapper.selectDataByIdCard(year, quarter, idCardList);
    }

    @Override
    public List<PreviousPerformanceDataEn> selectSelective(Integer quarter, Integer year, String department, Integer teamGroup, String employeeInfo) {
        return mapper.selectSelective(quarter, year, department, teamGroup, employeeInfo);
    }

    @Override
    public CurrentPerformanceTotal selectDataTotal(Integer quarter, Integer year, String department, Integer teamGroup) {
        return mapper.selectDataTotal(quarter, year, department, teamGroup);
    }

    @Override
    public boolean updatePreviousData(PreviousPerformanceData previousPerformanceData) {
        // 判断该数据是否为当前季度的前一个季度。
        int year = previousPerformanceData.getYear();
        int quarter = previousPerformanceData.getQuarter();
        int nextYear = quarter == 4 ? year + 1 : year;
        int nextQuarter = quarter == 4 ? 1 : quarter + 1;

        if (currentMapper.selectCountByQuarter(nextYear, nextQuarter) >0) {
            // 是的话，就查询该员工下一个季度的数据，有的话，就更新。
            CurrentPerformanceData currentPerformanceData = currentMapper.selectDataByEmployee(nextYear, nextQuarter, previousPerformanceData.getEmployeeIdCard());
            if (currentPerformanceData != null) {
                currentPerformanceData.setCumulativeBase(previousPerformanceData.getCurrentBase());
                currentPerformanceData.setCurrentBase(previousPerformanceData.getCurrentBase() - currentPerformanceData.getValueOfPunishment() + currentPerformanceData.getIncreaseBase());
                return currentMapper.updateByPrimaryKeySelective(currentPerformanceData) > 0;
            }else {// 没有的话，就加上。
                CurrentPerformanceData currentPerformanceData1 = new CurrentPerformanceData();
                currentPerformanceData1.setYear(nextYear);
                currentPerformanceData1.setQuarter(nextQuarter);
                currentPerformanceData1.setEmployeeIdCard(previousPerformanceData.getEmployeeIdCard());
                currentPerformanceData1.setCumulativeBase(previousPerformanceData.getCurrentBase());
                if (employeeMapper.getIsDirectProducer(previousPerformanceData.getEmployeeIdCard())) {
                    currentPerformanceData1.setIncreaseBase(100.0);
                }else {
                    currentPerformanceData1.setIncreaseBase(50.0);
                }
                currentPerformanceData1.setProportionOfPunishment(0.0);
                currentPerformanceData1.setValueOfPunishment(0.0);
                currentPerformanceData1.setCurrentBase(currentPerformanceData1.getCumulativeBase() + currentPerformanceData1.getIncreaseBase());
                currentPerformanceData1.setMoneyEveryMonth(currentPerformanceData1.getCurrentBase());
                currentPerformanceData1.setMoneyQuarter(currentPerformanceData1.getCurrentBase() * 3);
                return currentMapper.insert(currentPerformanceData1) > 0;
            }
        }else {
            return true;
        }
    }

    @Override
    public List<PreviousPerformanceData> selectPreviousIsEmpty(Integer year, Integer quarter, String employee_idCard) {
        return mapper.selectPreviousIsEmpty(year, quarter, employee_idCard);
    }

    @Override
    public int deletePreviousPerformanceInfo(Integer year, Integer quarter, String employee_idCard) {
        return mapper.deletePreviousPerformanceInfo(year, quarter, employee_idCard);
    }
}
